package com.maiyawx.mall.handle;

import com.maiyawx.mall.entity.Result;
import com.maiyawx.mall.enums.CommonEnum;
import com.maiyawx.mall.exception.CommonException;
import com.maiyawx.mall.exception.WxException;
import com.maiyawx.mall.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常捕捉
 */
@ControllerAdvice
@Slf4j
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e)
    {
        if (e instanceof CommonException) {
            CommonException commonException = (CommonException) e;
            return ResultUtil.error(commonException.getCode(), commonException.getMessage());
        }

        if (e instanceof WxException) {
            WxException wxException = (WxException) e;
            return ResultUtil.error(wxException.getCode(), wxException.getMessage());
        }
        log.info("{}", e);
        return ResultUtil.error(CommonEnum.SYSTEM_ERROR);
    }
}
